//
//  WMMeetingViewController.m
//  witMorning
//
//  Created by Mac mini on 2018/1/4.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "WMMeetingViewController.h"
#import "NPHomeHeaderBar.h"
#import "WMFoodsHeadView.h"
#import "WMMeetingSimpleTableView.h"
#import "WMMeetingDetailTableView.h"

#define head_height 40
#define head_dateHeight 50
@interface WMMeetingViewController ()<homeHeaderBarDelegate>

@end

@implementation WMMeetingViewController
{
    NPHomeHeaderBar *headerBar;
 
    UIView *meetingView;
    WMMeetingSimpleTableView *simple;
    WMMeetingDetailTableView *detail;
    
    NSArray *metArray;
    NSMutableArray *roomArray;
    NSString *timeStr; //yyyy-MM-dd
    NSString *roomStr;
}

#pragma mark ----homeHeaderBarDelegate----
-(void)homeHeaderBarClickWithIndex:(NSInteger)index{
    if (index == 0) {
        self.webView.hidden = NO;
        meetingView.hidden = YES;
    }else{
        self.webView.hidden = YES;
        meetingView.hidden = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"智慧会议";
    [self createMeetingView];
    [self requestData];

    [self createBar];
    
    [self createTimeBar];
    
}

-(void)createTimeBar{
    WMFoodsHeadView *view = [[WMFoodsHeadView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT+head_height, SCREEN_W, head_dateHeight)];
    
    NSArray *array = [WMTool daysOfCurrentMonth];
    view.dateArray = array;
    [self.view addSubview:view];
    dispatch_async(dispatch_get_main_queue(), ^{

     [view selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    });
    
    view.FoodHeadCallBack = ^(NSInteger index) {
        NSString *str;
        if ([array[index] integerValue]<10) {
            str = [NSString stringWithFormat:@"-0%ld",[array[index] integerValue]];
        }else{
            str = [NSString stringWithFormat:@"-%ld",[array[index] integerValue]];
        }
        
        NSString *time = [[WMTool currrentWithFormatterStyle:@"yyyy-MM"] stringByAppendingString:str];
        timeStr = time;
        [self checkMeetingWithDate:time];
        [self checkMeetingDetail];
    };
    view.FoodHeadCallBack(0);
}

-(void)createBar{
    headerBar= [[NPHomeHeaderBar alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_W, head_height) andTitleArray:@[@"会议概况",@"会议详情"]];
    headerBar.barDelegate = self;
    [self.view addSubview:headerBar];
}

-(void)createMeetingView{
    if (!meetingView) {
        meetingView = [[UIView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT+head_height+head_dateHeight, SCREEN_W, SCREEN_H-(NAV_HEIGHT+head_height+head_dateHeight))];
        [self.view addSubview:meetingView];
        [meetingView addSubview:self.simple];
        [meetingView addSubview:self.detail];
        [self.view addSubview:meetingView];
        
        meetingView.hidden = NO;
    }
}


-(void)requestData{
    NSString *url = [BASE_HTTP stringByAppendingString:@"getBoardRoom"];
    [[WMManage shareInstance]getRequestWithUrl:url success:^(id responseObj) {
        NSLog(@"%@",responseObj);
       
        metArray = [NSArray arrayWithArray:responseObj];
        if (metArray==nil || metArray.count<1) {

            return ;
        }
        if (!roomArray) {
            roomArray = [NSMutableArray arrayWithCapacity:0];
            if (roomArray.count == 0) {
                [roomArray addObject:metArray[0][@"holdPlace"]];
                roomStr = metArray[0][@"holdPlace"];
            }
            
        }
        for (NSDictionary *dic in metArray) {
            [roomArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj isEqualToString:dic[@"holdPlace"]]) {
                    *stop = YES;
                }else if (idx == roomArray.count-1){
                    [roomArray addObject:dic[@"holdPlace"]];
                    *stop = YES;
                }
            }];
        }
        simple.array = roomArray;
        [simple reloadData];
        simple.simpleCallBack(0);
//        [simple selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    } fail:^(id error) {
        NSLog(@"%@",error);
    }];
}

-(void)checkMeetingWithDate:(NSString *)date{
    NSString *str = [@"http://221.226.124.194:8950/zhcg/view/sys/hy.jsp?date=" stringByAppendingString:date];
    self.netHtml = str;
    if (!self.webView) {
        [self loadHTML];
        self.webView.frame = CGRectMake(0, NAV_HEIGHT+head_height+head_dateHeight, SCREEN_W, SCREEN_H-(NAV_HEIGHT+head_height+head_dateHeight));
        self.webView.hidden = NO;
    }else{
        [self loadHTML];
    }
}

-(void)checkMeetingDetail{
    if (roomStr && timeStr) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        NSInteger time = [[timeStr stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue];
        [metArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger finish = [[[obj[@"finishDate"] substringToIndex:10] stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue];
            NSInteger hold = [[[obj[@"holdDate"] substringToIndex:10] stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue];
            
            if ((hold<=time && time<=finish) && [obj[@"holdPlace"] isEqualToString:roomStr]) {
                [array addObject:obj];
            }
        }];
        
        detail.array = array;
        [detail reloadData];
    }
}

-(WMMeetingSimpleTableView *)simple{
    if (!simple) {
        simple = [[WMMeetingSimpleTableView alloc]initWithFrame:CGRectMake(0, 0, 80,CGRectGetHeight(meetingView.frame)) style:UITableViewStyleGrouped];
        if (@available(iOS 11.0, *)) {
            simple. contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        __block typeof(self) blockS = self;
        simple.simpleCallBack = ^(NSInteger index) {
            
            if (roomArray.count > 0) {
                roomStr = roomArray[index];
                [blockS checkMeetingDetail];

            }

          
        };
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [simple selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    });
    
    return simple;
}

-(WMMeetingDetailTableView *)detail{
    if (!detail) {
        detail = [[WMMeetingDetailTableView alloc]initWithFrame:CGRectMake(80, 0,SCREEN_W-  80,CGRectGetHeight(meetingView.frame)) style:UITableViewStyleGrouped];
        if (@available(iOS 11.0, *)) {
            detail. contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return detail;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
